
$Core.follow =
{
    sUrl: '',
    bIsFollow: false,
    bIsCurrentUser: false,
    iUserId: 0,
    bIsAddButtonFollow: false,
    type:'',
    item_id: 0,
    url: function(sUrl)
    {
        this.sUrl = sUrl;
    },
    init :function()
    {
        $('.follow-inline-btn').unbind('click').bind('click', function(){
            $sHref = $(this).attr('href');
            $sControllerName = oParams['sController'];
            $.ajaxCall('follow.parseFunction','url='+$sHref+'&controller='+$sControllerName);
            
            return false;
        });
    },
    changeSetting: function(ele)
    {
        $(ele).ajaxCall('follow.changeSetting');
    },
    confirmFollow: function($sType, $iItemId, $iUserId, $iNotiId)
    {
        $.ajaxCall('follow.confirmFollow', 'user_id='+$iUserId+'&noti_id='+$iNotiId+'&type='+$sType+'&item_id='+$iItemId);
    },
    denyFollow: function($sType, $iItemId, $iUserId, $iNotiId)
    {
        $.ajaxCall('follow.denyFollow', 'user_id='+$iUserId+'&noti_id='+$iNotiId+'&type='+$sType+'&item_id='+$iItemId);
    },
    confirmFollowIndex: function($sType, $iItemId, $iUserId, $iPendingId)
    {
        $.ajaxCall('follow.confirmFollowIndex', 'user_id='+$iUserId+'&pending_id='+$iPendingId+'&type='+$sType+'&item_id='+$iItemId);
    },
    denyFollowIndex: function($sType, $iItemdId, $iUserId, $iPendingId)
    {
        $.ajaxCall('follow.denyFollowIndex', 'user_id='+$iUserId+'&pending_id='+$iPendingId+'&type='+$sType+'&item_id='+$iItemdId);
    },
    changeApproval: function($iItemId, $iStatus, $sType)
    {
          $.ajaxCall('follow.changeApproval','status='+$iStatus+'&type='+$sType+'&item_id='+$iItemId);
    },
    getInfoFollow: function($iUserId, $bIsFollow, $bIsCurrentUser, $sType, $iItemId)
    {
        
        this.iUserId = parseInt($iUserId);
        this.bIsFollow = $bIsFollow;
        this.bIsCurrentUser = $bIsCurrentUser;
        this.type = $sType;
        this.item_id = parseInt($iItemId);
 
    },
    setPhrases: function($sFollow, $sUnfollow, $sFollowing)
    {
        oTranslations['follow.follow'] = $sFollow;
        oTranslations['follow.unfollow'] = $sUnfollow;
        oTranslations['follow.following'] = $sFollowing;
    },
    showSection: function(oObj, sType)
    {
        sType = trim(sType);
        $('#follower_'+sType).slideUp();
        $('#js_pager_view_more_link_follower').hide();
        $('#following_'+sType).slideUp();
        $('#js_pager_view_more_link_following').hide();
        $('#blocker_'+sType).slideUp();
        $('#js_pager_view_more_link_blocker').hide();
        $(oObj+'_'+sType).show('medium');
        oObj=oObj.replace('#','');
        
        iCurrent = parseInt($('#current_'+oObj).val());
        iTotal = parseInt($('#total_'+oObj).val());
        if(iTotal > iCurrent)
        {
            $('#js_pager_view_more_link_'+oObj).show(); 
        }
    },
    viewMore: function($sType)
    {
        $iPage = $('#follow_page_view_'+$sType).val();
        $iItemId = $('#follow_page_item_id').val();
        $sModule = $('#follow_module_id').val();
        $sKeyword = $('#follow_keyword').val();
        $.ajaxCall('follow.viewMore', 'page='+$iPage+'&type='+encodeURIComponent($sType)+'&item_id='+$iItemId+'&module_id='+$sModule+'&keyword='+encodeURIComponent($sKeyword), 'GET');
    },
    setPage:function($iPage, $sType)
    {
        $iPage = parseInt($iPage) + 1;
        $('#follow_page_view_'+$sType).val($iPage);
    },
    updateCurrent:function(sType, iNumber)
    {
        iCurrent = $('#current_'+sType).val();
        iCurrent = parseInt(iCurrent) + parseInt(iNumber);
        $('#current_'+sType).val(iCurrent);
        iTotal = $('#total_'+sType).val();
        if(iTotal <= iCurrent)
        {
            $('#js_pager_view_more_link_'+sType).hide(); 
        }
    },
    hideViewMore: function($sType)
    {
        $('#js_pager_view_more_link_'+$sType).hide();
    },
    changeText: function ()
    {
        $('.follow-text').hover(function()
        {
            $(this).html(oTranslations['follow.unfollow']);
        }, 
            function(){
            $(this).html(oTranslations['follow.following']);

     }); 
    },
    loadFollowButton: function()
    {
        if(this.item_id && this.type && this.iUserId)
        {
            if(this.bIsAddButtonFollow == false)
            {
                if(!parseInt(this.bIsCurrentUser) || this.bIsCurrentUser == false)
                {
                    if(this.bIsFollow == 1 || this.bIsFollow == true)
                    {
                        if(!$Core.exists('#section_menu > ul'))
                        {
                            $('#section_menu').prepend('<ul></ul>');
                        }    
                        
                        $sHtml = '\<li\>\<a class="no_border follow-inline-btn unfollow_'+this.type+'_'+this.item_id+'" href="'+this.sUrl+'del/id_'+this.iUserId+'/type_'+this.type+'/oid_'+this.item_id+'/"><span class="following follow-button follow-text" style="float:right;">'+oTranslations['follow.following']+'\<\/span\>\<\/a\>\<\/li\>';    
                        $sHtml += '\<li\>\<a style ="display:none" class="no_border follow-inline-btn follow_'+this.type+'_'+this.item_id+'" href="'+this.sUrl+'add/id_'+this.iUserId+'/type_'+this.type+'/oid_'+this.item_id+'/"><span class="following follow-button" style="float:right;">'+oTranslations['follow.follow']+'\<\/span\>\<\/a\>\<\/li\>'; 
                        $('#section_menu').find('ul').append($sHtml);     
                    }
                    else
                    {
                        
                        if(!$Core.exists('#section_menu > ul'))
                        {
                           $('#section_menu').prepend('<ul></ul>'); 
                        }
                        $sHtml = '\<li\>\<a class="no_border follow-inline-btn follow_'+this.type+'_'+this.item_id+'" href="'+this.sUrl+'add/id_'+this.iUserId+'/type_'+this.type+'/oid_'+this.item_id+'/"><span class="following follow-button" style="float:right;">'+oTranslations['follow.follow']+'\<\/span\>\<\/a\>\<\/li\>';
                        $sHtml += '\<li\>\<a style="display:none" class="no_border follow-inline-btn unfollow_'+this.type+'_'+this.item_id+'" href="'+this.sUrl+'del/id_'+this.iUserId+'/type_'+this.type+'/oid_'+this.item_id+'/"><span class="following follow-button follow-text" style="float:right;">'+oTranslations['follow.following']+'\<\/span\>\<\/a\>\<\/li\>';   
                        $('#section_menu').find('ul').append($sHtml);       
                    }
                }
                this.bIsAddButtonFollow = true; 
            }
        }
        $Core.follow.init();
    },
    reloadNotification: function($iTimeOut)
    {
        setTimeout("$.ajaxCall('follow.reloadNotification', 'newest-id=' + $Core.follow.getNewestId(), 'GET');", $iTimeOut * 1000);
    },
    getNewestId: function()
    {
        return $('#follow_noti_newest_id').attr('value');
    },
    updateNewestId: function($iId)
    {
        $('#follow_noti_newest_id').attr('value',$iId);
        //$('.follow_notification_content.follow_ajax_load').animate({'top': '-100px'}, 1000);
    },
    updateNotification: function($iId, $sHtml)
    {
        $('#follow_noti_newest_id').attr('value',$iId);
        if($Core.exists('.follow_notification_content.follow_ajax_load'))
        {
            $('.follow_notification_content.follow_ajax_load').animate({'top': '-100px'}, 1500);    
        }
        
        setTimeout(function(){
            $('.follow_notification_content.follow_ajax_load').remove();
            $('body').append($sHtml);
        }, 1500);
        
        setTimeout(function(){
            $('.follow_notification_content.follow_ajax_load').fadeOut(2000);
        }, 6000);
    },
    closeFollowNotification: function()
    {
        $('.follow_notification_content.follow_ajax_load').remove();
        return false;
    }
}
$Behavior.loadfollow = function()
{
    sType = window.location.hash;
    if(oParams['sController'] == 'follow.index')
    {
        $(sType+'_user').show('medium');    
    }
    sType = sType.replace('#','');
    iCurrent = parseInt($('#current_'+sType).val());
    iTotal = parseInt($('#total_'+sType).val());
    if(iTotal > iCurrent)
    {
        $('#js_pager_view_more_link_'+sType).show(); 
    }
    //if(oParams['sController'] == 'profile.index' || oParams['sController'] == 'pages.view' || oParams['sController'] == 'pages.info')
    {
        $Core.follow.loadFollowButton();        
    }
     $('.follow-text').hover(function()
        {
            $(this).html(oTranslations['follow.unfollow']);
        }, 
            function(){
            $(this).html(oTranslations['follow.following']);

     }); 
}

